<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #toolbar>
        <a-button type="primary" @click="handleAdd">新增</a-button>
        <a-button type="primary" @click="handleChangeBusNum(selectedRow)" :disabled="!hasSelectedRows" >评估充值</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
    </BasicTable>
    <SalesManUserDrawer @register="registerSalesManUserDrawer" @success="reload"/>
    <UserChangeBusNumModal @register="registerUserChangeBusNumModalModal" @success="handleSuccess"/>
  </div>
</template>
<script lang="ts" setup name="appUser">
import {ActionItem, BasicTable, TableAction} from '@apps/antd-admin/src/components/Table';
import {useDrawer} from '@apps/antd-admin/src/components/Drawer';
import {useBaseTable} from '@apps/antd-admin/src/hooks';
import {columns, searchFormSchema} from './data/data';
import {Api, resetPwd} from './data/api';
import {useModal} from '@apps/antd-admin/src/components/modal';
import UserChangeBusNumModal from './components/UserChangeBusNumModal.vue';
import SalesManUserDrawer from './components/SalesManUserDrawer.vue';
const {tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete} = useBaseTable({
  designScope: 'appUser-template',
  tableProps: {
    //表格标题
    title: '业务员列表',
    //主键字段
    rowKey: 'userId',
    //列表查询handler
    handlerName: 'salesManUserInfoListHandler',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    //默认排序(排序字段上需设置defaultSortOrder:'descend')
    defSort: {
      order: 'user.createTime',
      sort: 'desc',
    },
    //操作列配置
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      slots: {customRender: 'action'},
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});
const [registerTable, {reload}, {rowSelection, hasSelectedRows, selectedRow}] = tableContext;
const [registerSalesManUserDrawer, {openDrawer: openUserFormDrawer}] = useDrawer();
const [registerUserChangeBusNumModalModal, {openModal: openUserChangeBusNumModal}] = useModal();


/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      auth: 'ACTION_editAppUser',
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        icon: 'ant-design:delete-outlined',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '重置密码',
      popConfirm: {
        title: '确认重置密码为123456789吗?',
        icon: 'ant-design:delete-outlined',
        confirm: resetPwd.bind(null, record.userId),
      },
    }
  ];
}

function handleEdit(record: Recordable) {
  openUserFormDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleChangeBusNum(record: Recordable) {
  openUserChangeBusNumModal(true, {
    record,
    isUpdate: true,
  });
}

function handleAdd() {
  openUserFormDrawer(true, {
    isUpdate: false,
    showFooter: true,
  });
}
</script>
